package com.group2.view;

import model.Student;

import java.awt.Font;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.io.*;
import java.net.Socket;
import java.util.Objects;
import static model.netBase.IP;

import javax.swing.*;

import model.*;

import static java.lang.Boolean.valueOf;

public class StudentMainFrame_User extends JFrame {
	// 声明对象
	//JFrame jf3=new JFrame();
	JPanel panel = new JPanel(null);// 第一个JPanel
	
	JLabel intro = new JLabel("您的学籍信息如下:");
	JLabel studentsname = new JLabel("姓名：");
	JLabel studentsnumber = new JLabel("学号：");
	JLabel studentssex = new JLabel("性别：");
	JLabel studentsgrade = new JLabel("年级：");
	JLabel studentsbirthplace = new JLabel("籍贯：");
	JLabel studentsmajor = new JLabel("专业：");
	JLabel studentsschool = new JLabel("学院：");
	private Timer timer;
	// public JFrame index;
	Font d = new Font("黑体", Font.BOLD, 17);
	Font f = new Font("黑体", Font.PLAIN, 15);

	// 构造函数
	public StudentMainFrame_User(String name, Timer time) {
		init(name);
		timer=time;
		time.start();
		this.addMouseMotionListener(new MouseMotionListener() {
			@Override
			public void mouseDragged(MouseEvent e) {

			}

			@Override
			public void mouseMoved(MouseEvent e) {
				time.stop();
			}
		});
		this.addMouseListener(new MouseListener() {
			@Override
			public void mouseClicked(MouseEvent e) {

			}

			@Override
			public void mousePressed(MouseEvent e) {

			}

			@Override
			public void mouseReleased(MouseEvent e) {

			}

			@Override
			public void mouseEntered(MouseEvent e) {
				time.start();
			}

			@Override
			public void mouseExited(MouseEvent e) {
				time.start();
			}
		});
	}

	private void init(String id) {
		// 初始化框架
		// 设置框架大小及位置
		this.setSize(450, 400);
		this.setTitle("欢迎学生查看学籍！");
		this.setResizable(false);
		//this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		// 居中显示
		JLabel icon = new JLabel();
		icon.setIcon(new javax.swing.ImageIcon(UserLogOnFrame.class.getResource("/images/student.png")));
		icon.setBounds(50, 50, 30, 30);
		
		
		JLabel name = new JLabel();
		JLabel number = new JLabel();
		JLabel gender = new JLabel();
		JLabel grade = new JLabel();
		JLabel birthplace = new JLabel();
		JLabel major = new JLabel();
		JLabel school = new JLabel();
		//获取学籍信息
		number.setText(id);
		try
		{
			Socket socket = new Socket(IP(), 2000);
			//设置超时时间
			socket.setSoTimeout(3000);
			//构建IO
			InputStream is = socket.getInputStream();
			OutputStream os = socket.getOutputStream();
			//向服务器传输操作码
			BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
			//传输当前用户的学号
			bw.write("31"+'\n'+id+'\n');
			bw.flush();
			//建立输入输出对象流
			ObjectInputStream ois=new ObjectInputStream(is);
			//接受学籍信息对象
			Object obj = ois.readObject();
			Student student=(Student)obj;
			//获取信息
			name.setText(student.getName());
			if(Objects.equals(student.getSex(), "1")) {
				gender.setText("男");
			}
			else{
				gender.setText("女");
			}
			major.setText(student.getMajor());
			grade.setText(student.getGrade()+"");
			birthplace.setText(student.getBirthplace());
			school.setText(student.getSchool());
			//关闭流
			is.close();
			os.close();
			socket.close();
		}catch (Exception e) {
			System.out.println("连接异常！");
		}
		panel.setBounds(0, 0, 450, 500);

		intro.setBounds(80, 50, 300, 30);
		intro.setFont(d);
		studentsname.setBounds(50, 100, 80, 20);
		studentsname.setFont(f);
		name.setBounds(100, 100, 100, 20);
		name.setFont(f);

		studentsnumber.setBounds(50, 140, 80, 20);
		studentsnumber.setFont(f);
		number.setBounds(100, 140, 100, 20);
		number.setFont(f);

		studentssex.setBounds(220, 140, 80, 20);
		studentssex.setFont(f);
		gender.setBounds(270, 140, 100, 20);
		gender.setFont(f);

		studentsgrade.setBounds(50, 180, 80, 20);
		studentsgrade.setFont(f);
		grade.setBounds(100, 180, 100, 20);
		grade.setFont(f);

		studentsbirthplace.setBounds(220, 180, 80, 20);
		studentsbirthplace.setFont(f);
		birthplace.setBounds(270, 180, 100, 20);
		birthplace.setFont(f);

		studentsmajor.setBounds(50, 220, 80, 20);
		studentsmajor.setFont(f);
		major.setBounds(100, 220, 100, 20);
		major.setFont(f);

		studentsschool.setBounds(220, 220, 80, 20);
		studentsschool.setFont(f);
		school.setBounds(270, 220, 100, 20);
		school.setFont(f);

		panel.add(icon);
		panel.add(intro);
		panel.add(studentsname);
		panel.add(name);
		panel.add(studentsnumber);
		panel.add(number);
		panel.add(studentssex);
		panel.add(gender);
		panel.add(studentsgrade);
		panel.add(grade);
		panel.add(studentsbirthplace);
		panel.add(birthplace);
		panel.add(studentsmajor);
		panel.add(major);
		panel.add(studentsschool);
		panel.add(school);

		this.getContentPane().add(panel);
		this.setVisible(true);
		this.setLocationRelativeTo(null);
		//将panel放入jframe界面
	}

	// 给所有的菜单条目设置监听事件

//	public static void main(String[] args) {
//		new StudentMainFrame_User("abc");
//	}
}